﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net.Mail;
using System.Net;

namespace Common.Net.Mail
{
    public static class SmtpMail
    {
        public static void SendTextEmail(string SmtpServerName, int SmtpPort, string[] SendTo, string FromLine, string[] Bcc,string Subject, string MessageBody)
        {
            if (String.IsNullOrEmpty(SmtpServerName)) return;
            if (String.IsNullOrEmpty(Subject)) return;
            if (String.IsNullOrEmpty(MessageBody)) return;
            if (String.IsNullOrEmpty(FromLine)) return;
            if (SmtpPort < 1) return;
            if (SendTo.isNull() || SendTo.Count() < 1) return;

            SmtpClient sClient = new SmtpClient(SmtpServerName, SmtpPort);
            MailMessage msgBody = new MailMessage();

            msgBody.IsBodyHtml = false;
            msgBody.HeadersEncoding = Encoding.UTF8;
            msgBody.BodyEncoding = Encoding.UTF8;
            msgBody.SubjectEncoding = Encoding.UTF8;

            SendTo.ForEach(p => msgBody.To.Add(p));
            
            if (Bcc.isNull() == false && Bcc.Count() > 0)
                Bcc.ForEach(p => msgBody.Bcc.Add(p));

            msgBody.Body = MessageBody;
            msgBody.From = new MailAddress(FromLine);
            msgBody.Subject = Subject;


            //sClient.EnableSsl = false;
            //sClient.UseDefaultCredentials = true;
            sClient.Send(msgBody);
        }

        public static void SendTextEmail(string SmtpServerName, int SmtpPort, NetworkCredential Credentials, string[] SendTo, string FromLine, string[] Bcc, string Subject, string MessageBody)
        {
            if (Credentials.isNull()) return;
            if (String.IsNullOrEmpty(SmtpServerName)) return;
            if (String.IsNullOrEmpty(Subject)) return;
            if (String.IsNullOrEmpty(MessageBody)) return;
            if (String.IsNullOrEmpty(FromLine)) return;
            if (SmtpPort < 1) return;
            if (SendTo.isNull() || SendTo.Count() < 1) return;

            SmtpClient sClient = new SmtpClient(SmtpServerName, SmtpPort);
            MailMessage msgBody = new MailMessage();

            msgBody.IsBodyHtml = false;
            msgBody.HeadersEncoding = Encoding.UTF8;
            msgBody.BodyEncoding = Encoding.UTF8;
            msgBody.SubjectEncoding = Encoding.UTF8;

            SendTo.ForEach(p => msgBody.To.Add(p));

            if (Bcc.isNull() == false && Bcc.Count() > 0)
                Bcc.ForEach(p => msgBody.Bcc.Add(p));

            msgBody.Body = MessageBody;
            msgBody.From = new MailAddress(FromLine);
            msgBody.Subject = Subject;


            //sClient.EnableSsl = false;
            sClient.Credentials = Credentials;
            //sClient.UseDefaultCredentials = false;
            sClient.Send(msgBody);
        }
    }
}
